<template>
  <div class="box">
    <div class="head-fixed">
      <div class="head">
        <div class="head-div">
          <img src="../../assets/zjt2.png" alt="" class="back" @click="back()"/>
        </div>
        <div class="head-search">个人信息</div> <!--v-show="status == 0"-->
        <div @click="saveIndividual">保存</div>
      </div>
    </div>
    <div class="content">
      <div class="inpBox">
        <span>头像</span>
        <input
          v-show="false"
          type="file"
          accept="image/*"
          @change="tirggerFile($event)"
          ref="input"
        />
        <div>
          <img
            src="../../assets/avatar.png"
            v-if="imgUrl === ''"
            alt=""
            @click="openImg"
          />
          <img
            :src="imgUrl"
            alt=""
            v-if="imgUrl !== ''"
            @click="openImg"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>姓名</span>
        <div>
          <input
            type="text"
            v-model="individualData.userName"
            placeholder="请输入姓名"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>性别</span>
        <div>
          <input
            type="radio"
            name="sex"
            v-model="individualData.sex"
            value="0"
          /><span>男</span>
          <input
            type="radio"
            name="sex"
            v-model="individualData.sex"
            value="1"
          /><span>女</span>
        </div>
      </div>
      <div class="inpBox">
        <span>出生日期</span>
        <div>
          <input
            type="date"
            v-model="individualData.birthdate"
            placeholder="请输入出生日期"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>电话</span>
        <div>
          <input
            type="text"
            v-model="individualData.tel"
            placeholder="请输入电话号码"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>微信</span>
        <div>
          <input
            type="text"
            v-model="individualData.wechat"
            placeholder="请输入微信"
          />
        </div>
      </div>
      <div class="inpBox">
        <span>邮箱</span>
        <div>
          <input
            type="text"
            v-model="individualData.email"
            placeholder="请输入邮箱"
          />
        </div>
      </div>
      <div class="inpBoxs">
        <div class="inpBox">
          <span>个人简介</span>
          <div style="color: #757575">选填</div>
        </div>
        <textarea
          name=""
          id=""
          placeholder="请描述你的个人经历..."
          v-model="individualData.userIndividual"
        ></textarea>
      </div>
    </div>
  </div>
</template>

<script>
import Vue from 'vue'
import {Toast} from 'mint-ui'

export default {
  inject: ['reload'],
  name: 'usersInformation',
  components: {},
  created () {
    // created 在模板渲染成html前调用，即通常初始化某些属性值，然后再渲染成视图
    this.getInformationData()
  },
  mounted () {
    // mounted在模板渲染成html后调用，通常是初始化页面完成后，再对html的dom节点进行一些需要的操作
  },
  // data() 为变量赋值等
  data () {
    return {
      // status为用户身份，0是求职者，1是招聘者
      status: localStorage.status,
      imgUrl: '',
      check: 0,
      name: '',
      birthday: '2020-01-01',
      phone: '12345678900',
      wx: '123456789',
      email: '123456789@qq.com',
      text: '',
      individualData: {},
      userPortrait: '',
    }
  },
  // methods 编写js函数
  methods: {
    notEmpty (val) {
      const re = new RegExp('^[ ]+$') //正则，去除空字符。匹配是否为空
      return !(val == null || val === '' || typeof (val) === 'undefined' || re.test(val))
    },
    saveIndividual () {
      if (!this.notEmpty(this.individualData.userPortrait)) {
        return Toast('请选择头像')
      }
      if (!this.notEmpty(this.individualData.userName)) {
        return Toast('请输入姓名')
      }
      if (!this.notEmpty(this.individualData.sex)) {
        return Toast('请选择性别')
      }
      if (!this.notEmpty(this.individualData.birthdate)) {
        return Toast('请选择出生日期')
      }
      if (!this.notEmpty(this.individualData.tel)) {
        return Toast('请输入电话')
      }
      if (this.individualData.tel.length !== 11) {
        return Toast('请输入正确的电话')
      }
      if (!this.notEmpty(this.individualData.wechat)) {
        return Toast('请输入微信')
      }
      if (!this.notEmpty(this.individualData.email)) {
        return Toast('请输入邮箱')
      }

      this.$http({
        method: 'post',
        url: 'api/user/updateUserInfo',
        data: {
          id: this.individualData.userId,
          userPortrait: this.individualData.userPortrait,
          email: this.individualData.email,
          userName: this.individualData.userName,
          sex: this.individualData.sex,
          birthdate: this.individualData.birthdate,
          mobile: this.individualData.tel,
          userIndividual: this.individualData.userIndividual,
          weixin: this.individualData.wechat,
          age: this.getAge(this.individualData.birthdate)
        },
      }).then(({data}) => {
        if (data && data.code === 0) {
          alert('保存成功')
          // this.$router.push('/usersResume')
          this.$router.go(-1)
        }
      })
    },
    getInformationData () {
      this.$http({
        method: 'get',
        url: 'api/user/getUserInfo/' + window.localStorage.getItem('userId'),
        params: {},
      }).then(({data}) => {
        this.individualData = data.user
        this.imgUrl = Vue.prototype.AppURL + '/file/' + data.user.resumePortrait
        this.individualData.userPortrait = data.user.resumePortrait
        console.log(this.individualData)
      })
    },
    back () {
      this.$router.go(-1) //返回上一层
    },
    tirggerFile: function (event) {
      let file = event.target.files[0]
      let url = ''
      var reader = new FileReader()
      reader.readAsDataURL(file)
      let that = this
      reader.onload = function (e) {
        url = this.result.substring(this.result.indexOf(',') + 1)
        that.imgUrl = 'data:image/png;base64,' + url
        that.individualData.userPortrait = url//获取文件流，获取base64
      }
    },
    openImg () {
      this.$refs.input.click()
    },
    getAge (strBirthday) {
      var returnAge//返回一个年龄

      var strBirthdayArr = strBirthday.split('-')//中间的-
      var birthYear = strBirthdayArr[0]//定义年的数组
      var birthMonth = strBirthdayArr[1]//定义月的数组
      var birthDay = strBirthdayArr[2]//定义日的数组

      var d = new Date()//new出日期
      var nowYear = d.getFullYear() //这个nowYear是调用了getfullyear的底层方法
      var nowMonth = d.getMonth() + 1
      var nowDay = d.getDate()

      if (nowYear == birthYear) {
        returnAge = 0//同年 则为0岁
      } else {
        var ageDiff = nowYear - birthYear //年之差
        if (ageDiff > 0) { //
          if (nowMonth == birthMonth) {
            var dayDiff = nowDay - birthDay//日之差
            if (dayDiff < 0) {
              returnAge = ageDiff - 1//
            } else {
              returnAge = ageDiff
            }
          } else {
            var monthDiff = nowMonth - birthMonth//月之差
            if (monthDiff < 0) {//小于0说明生日的月份大
              returnAge = ageDiff - 1//相对的额实际年龄要-1
            } else {
              returnAge = ageDiff
            }
          }
        } else {
          returnAge = -1//出生日期不能大于今天
        }
      }
      return returnAge
    },
  },
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
html,
body,
.tac,
.box,
.tab-left,
.tab-left .el-menu {
  height: 100%;
}

.box {
  width: 100%;
  background: #fff;
}

.head-fixed {
  width: 100%;
  height: 43px;
  position: fixed;
  z-index: 999;
}

.head {
  background: #3e63a8;
  color: #fff;
  width: 100%;
  box-sizing: border-box;
  padding: 10px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 12px;
}

.back {
  width: 14px;
  height: 14px;
}

.head div img {
  width: 20px;
  height: 20px;
}

.head-search {
  box-sizing: border-box;
  flex: 1;
  text-align: center;
  font-size: 14px;
}

.content {
  position: relative;
  top: 43px;
  width: 100%;
  box-sizing: border-box;
  padding: 0 20px;
}

.inpBox {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  border-bottom: 1px solid #eee;
  justify-content: space-between;
  align-items: center;
  padding: 10px 0;
  font-size: 14px;
}

.inpBox img {
  width: 50px;
  height: 50px;
  border-radius: 50%;
}

.inpBox input {
  border: 0;
  flex: 1;
  outline: none;
  font-size: 14px;
  height: 30px;
  text-align: right;
}

.inpBox div {
  display: flex;
  align-items: center;
}

.inpBox div input[type="radio"] {
  margin-left: 20px;
  margin-right: 5px;
}

.inpBoxs {
  width: 100%;
  box-sizing: border-box;
  display: flex;
  flex-flow: column;
  justify-content: space-between;
  align-items: center;
  font-size: 14px;
}

.inpBoxs .inpBox {
  border: 0;
}

.inpBoxs textarea {
  width: 100%;
  height: 150px;
  margin-top: 10px;
  box-sizing: border-box;
  padding: 10px;
}
</style>
